<?php
/**
 * User: hufeng
 * Date: 2017/10/11
 * Desc: 用户管理
 */

namespace Admin\Controller;
use Model\UserModel;
use Model\UserExtModel;
use Model\UserAssetsModel;
use Model\OrderModel;
use Model\StockModel;
use Model\TradeUserAssetModel;

class UserController extends BaseController{
    public $userModel;
    public function __construct(){
        parent::__construct();
        $this->userModel = new UserModel();
    }

    //用户列表
    public function index(){
        if($_POST['__hash__'] || !$_GET['p']){
            session('searchParam',null);
        }
        $sessionParam = session('searchParam');

        $sessionArr['status']  = @$_POST['status'] ? $_POST['status'] : @$sessionParam['status'];
        $sessionArr['name']  = @$_POST['name'] ? $_POST['name'] : @$sessionParam['name'];
        $sessionArr['tel']  = @$_POST['tel'] ? $_POST['tel'] : @$sessionParam['tel'];
        $sessionArr['ip']  = @$_POST['ip'] ? $_POST['ip'] : @$sessionParam['ip'];
        $sessionArr['start']  = @$_POST['start'] ? $_POST['start'] : @$sessionParam['start'];
        $sessionArr['end']  = @$_POST['end'] ? $_POST['end'] : @$sessionParam['end'];
        session('searchParam',$sessionArr);
        $page = @$_GET['p'] ? $_GET['p'] : 1;

        $where = [];
        if($sessionArr['status']){
            $where['status'] = $sessionArr['status'];
        }
        if($sessionArr['name']){
            $where['real_name'] = $sessionArr['name'];
        }
        if($sessionArr['tel']){
            $where['tel'] = array('like','%'.$sessionArr['tel'].'%');
        }
        if($sessionArr['ip']){
            $where['ip'] = $sessionArr['ip'];
        }
        if($sessionArr['start'] && $sessionArr['end']){
            $where['_string'] = "addtime>='{$sessionArr['start']}' and addtime<='{$sessionArr['end']} 23:59:59'";
        }

        //所有股票
        $sModel = new StockModel();
        $sList = $sModel->selectData(['status'=>['neq',3]],['id','stock_no','stock_name']);
        $sresArr = $snameArr = [];
        foreach ($sList as $val){
            $sname = $val['stock_name'].'('.$val['stock_no'].')';
            $snameArr[] = $sname;
            $sresArr[$sname] = $val['id'];
        }

        $resList = $this->userModel->search($where,$page,$this->pernum,'Id desc');
        $totalNum = $this->userModel->getTotalNum($where);

        $pageShow = getPage($totalNum,$this->pernum);
        $this->assign('pageShow',$pageShow);
        $this->assign('page',$page);
        $this->assign('resList',$resList);
        $this->assign('totalNum',$totalNum);
        $this->assign('postArr',$sessionArr);
        $this->assign('snameArr',json_encode($snameArr,JSON_UNESCAPED_UNICODE));
        $this->assign('sresArr',json_encode($sresArr,JSON_UNESCAPED_UNICODE));
        $this->display('user/index');
    }
    //冻结解冻
    public function freezeUser(){
        $data['status'] = $_GET['status'] == 1 ? 2: 1;
        $where['id'] = $_GET['id'];
        $this->userModel->editData($where,$data);
        $this->logModel->log('冻结|解冻用户','customer',$_GET['id']);
        redirect($this->domianUrl.'/user/index?p='.$_GET['page'].'&tip=1');
    }
    //用户信息
    public function info(){
        //获内容
        $this->assign('page',$_GET['page']);
        $uid = $_GET['id'];
        //基本信息
        $uRes = $this->userModel->findData(['id'=>$uid]);
        //财务信息
        $uaModel = new UserAssetsModel();
        $userBalance = $uaModel->userBalance($uid);
        //扩展信息
        $ueModel = new UserExtModel();
        $ueRes = $ueModel->userInfo($uid);

        $this->assign('ueRes',$ueRes);
        $this->assign('uRes',$uRes);
        $this->assign('userBalance',$userBalance);
        $this->display('user/info');
    }
    //用户信息变更（基本信息，财务信息，认证信息,）
    public function infoEdit(){
        $this->assign('page',$_GET['page']);
        $uid = $_GET['id'];
        //基本信息
        $uRes = $this->userModel->findData(['id'=>$uid]);
        //财务信息
        $uaModel = new UserAssetsModel();
        $userBalance = $uaModel->userBalance($uid);
        //扩展信息
        $ueModel = new UserExtModel();
        $ueRes = $ueModel->userInfo($uid);

        $this->assign('ueRes',$ueRes);
        $this->assign('uRes',$uRes);
        $this->assign('userBalance',$userBalance);
        $this->display('user/infoedit');
    }
    //修改登录密码
    public function editLoginPwd(){
        $uid = $_POST['uid'];
        $lpwd = $_POST['lpwd'];
        $password = md5($lpwd);
        $this->userModel->editData(['id'=>$uid],['passwd'=>$password]);
        $this->logModel->log('修改用户登录密码','customer',$uid);
        $this->printOut(['code'=>0,'msg'=>'ok']);
    }
    //修改数字密码
    public function editSafePwd(){
        $uid = $_POST['uid'];
        $safepwd = $_POST['safepwd'];
        $password = md5($safepwd);
        $this->userModel->editData(['id'=>$uid],['safe_passwd'=>$password]);
        $this->logModel->log('修改用户数字密码','customer',$uid);
        $this->printOut(['code'=>0,'msg'=>'ok']);
    }
    //充值资产
    public function rechargeAsset(){
        $typeArr = ['money'=>BALANCE_MONEY,'coin'=>BALANCE_COIN];
        $uid = $_POST['uid'];
        $type = $typeArr[$_POST['type']];
        $money = $_POST['money'];
        $memo = $_POST['memo'] ? $_POST['memo'] : '管理后台充值';
        $uaModel = new UserAssetsModel();
        $res = $uaModel->addAsset($uid,$money,$type,1,$memo);
        $this->logModel->log('充值用户资产',$type,$uid);
        $this->printOut(['code'=>0,'msg'=>'ok','money'=>$res['money']]);
    }
    //扣除资产
    public function deducAsset(){
        $typeArr = ['money'=>BALANCE_MONEY,'coin'=>BALANCE_COIN];
        $uid = $_POST['uid'];
        $type = $typeArr[$_POST['type']];
        $money = $_POST['money'];
        $memo = $_POST['memo'] ? $_POST['memo'] : '管理后台扣款';

        $uaModel = new UserAssetsModel();
        $res = $uaModel->reducAsset($uid,$money,$type,1,$memo);
        if($res['code'] != 0){
            $this->printOut(['code'=>1,'msg'=>'用户账户资金不足']);
        }

        $this->logModel->log('充值用户资产',$type,$uid);
        $this->printOut(['code'=>0,'msg'=>'ok','money'=>$res['money']]);
    }
    //增持股份
    public function editUserStock(){
        $sModel = new StockModel();
        $sRes = $sModel->findData(['id'=>$_POST['sid']]);
        $tuaModel = new TradeUserAssetModel();
        $tuaModel->editUserHold($_POST['uid'],$_POST['sid'],$_POST['snum'],$sRes['begin_price'],'后台添加');
        exit;
    }
    //充值记录
    public function charge(){
        if($_POST['__hash__'] || !$_GET['p']){
            session('searchParam',null);
        }
        $sessionParam = session('searchParam');

        $sessionArr['status']  = @$_POST['status'] ? $_POST['status'] : @$sessionParam['status'];
        $sessionArr['name']  = @$_POST['name'] ? $_POST['name'] : @$sessionParam['name'];
        $sessionArr['tel']  = @$_POST['tel'] ? $_POST['tel'] : @$sessionParam['tel'];
        $sessionArr['start']  = @$_POST['start'] ? $_POST['start'] : @$sessionParam['start'];
        $sessionArr['end']  = @$_POST['end'] ? $_POST['end'] : @$sessionParam['end'];
        session('searchParam',$sessionArr);
        $page = @$_GET['p'] ? $_GET['p'] : 1;

        $where = [];
        $where['order_type'] = 2;
        if($sessionArr['status']){
            $where['pay_type'] = $sessionArr['status'];
        }
        if($sessionArr['name']){
            $uwhere = ['real_name'=>$sessionArr['name']];
            $uRes = $this->userModel->findData($uwhere);
            $where['uid'] = $uRes['id'] ? $uRes['id'] : 0;
        }
        if($sessionArr['tel']){
            $uwhere = ['tel'=>$sessionArr['tel']];
            $uRes = $this->userModel->findData($uwhere);
            $where['uid'] = $uRes['id'] ? $uRes['id'] : 0;
        }
        if($sessionArr['start'] && $sessionArr['end']){
            $where['_string'] = "addtime>='{$sessionArr['start']}' and addtime<='{$sessionArr['end']} 23:59:59'";
        }
        $oModel = new OrderModel();
        $resList = $oModel->search($where,$page,$this->pernum,'Id desc');
        $totalNum = $oModel->getTotalNum($where);
        $uidArr = [];
        foreach ($resList as $val){
            $uidArr[] = $val['uid'];
        }
        $uRes = [];
        if($uidArr){
            $uList = $this->userModel->selectData(['id'=>['in',$uidArr]],'id,tel,real_name');
            foreach ($uList as $val){
                $uRes[$val['id']]['tel'] = $val['tel'];
                $uRes[$val['id']]['rname'] = $val['real_name'];
            }
        }
        $pageShow = getPage($totalNum,$this->pernum);
        $this->assign('pageShow',$pageShow);
        $this->assign('page',$page);
        $this->assign('resList',$resList);
        $this->assign('totalNum',$totalNum);
        $this->assign('postArr',$sessionArr);
        $this->assign('uRes',$uRes);
        $this->assign('payType',C('orderPayType'));
        $this->display('user/charge');
    }
}